<?php

namespace App\Http\Controllers;

use Laravel\Lumen\Routing\Controller as BaseController;

class Controller extends BaseController
{
    /**
     * 返回格式化后的json响应
     * @param $data
     * @param int $code
     * @param null $msg
     * @return \Illuminate\Http\JsonResponse
     */
    protected function reply($data = null, $code = 200, $msg = null)
    {
        return $this->replyDirect($this->returnFormat($data, $code, $msg));
    }

    /**
     * 直接返回json响应
     * @param $obj
     * @return \Illuminate\Http\JsonResponse
     */
    protected function replyDirect($obj)
    {
        return response()->json($obj);
    }

    /**
     * 格式化返回
     * @param $data
     * @param int $code
     * @param null $msg
     * @return array
     */
    protected function returnFormat($data, $code = 200, $msg = null)
    {
        $obj = [
            "data" => $data,
            "code" => $code,
        ];
        if (empty($msg)) {
            $code_msg = trans('codeMsg');
            if (!empty($code_msg[$code])) $msg = $code_msg[$code];
        }
        $obj['msg'] = $msg;
        return $obj;
    }

}
